package co.za.alacrity.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;

import co.za.alacrity.factorypattern.ExFactory;
import co.za.alacrity.factorypattern.ProblemSolver;
import co.za.alacrity.obseverpattern.ButtonListener;
import co.za.alacrity.obseverpattern.TextFieldListener;
import co.za.alacrity.suduko.SudokuGame;
import java.awt.FlowLayout;
import java.util.Random;

public class Sudoku extends JFrame implements ButtonListener, TextFieldListener{
	
    private SudokuPanel sudokuPanel;
    private SudokuGame game;
    private SudokuGame origanal;
    private TextFields current;
    private buttonPanel buttonPaneln;
    private Color o;
	
	public Sudoku() {
        super("Sudoku");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        getContentPane().setLayout(new FlowLayout(FlowLayout.TRAILING, 5, 5));
        
        
        sudokuPanel = new SudokuPanel(this);
        buttonPaneln = new buttonPanel( this);
        
        JPanel gridPanel = new JPanel(new BorderLayout());
        gridPanel.add(sudokuPanel,BorderLayout.WEST);
        gridPanel.add(buttonPaneln,BorderLayout.EAST);
        getContentPane().add(gridPanel);
         
   
       
        setSize(520,420);
        setVisible(true);
        
	}
	
	
	@Override
	public void exit() {
		System.exit(0);
	}
	@Override
	public void restore() {
		if (current!=null)current.setColor(o);
		game=origanal.clone();
		mapSodukoToForm(game);
		System.out.println(origanal);
	}
	@Override
	public void generate() {
		ExFactory.instance.getSolver().solve(game);
		for (int r=0;r<9;r++){
			for (int k=0;k<9;k++){
				sudokuPanel.setValue(r,k,game.getSudokuValue(r,k));
			}
		}
	}
	
	@Override
	public void newGame() {
		SudokuGame s = new SudokuGame(ExFactory.instance.getSolver().genarator());
		origanal=s.clone();
		game=s;
		mapSodukoToForm(s);
	}
	
	public void mapSodukoToForm(SudokuGame s){
		
		for (int r=0;r<9;r++){
			for (int k=0;k<9;k++){
				sudokuPanel.setValue(r,k,0);
				sudokuPanel.setBold(r, k, false);
			}
		}
		for (int r=0;r<9;r++){
			for (int k=0;k<9;k++){
				sudokuPanel.setValue(r,k,s.getSudokuValue(r,k));
			}
		}
		for (int r=0;r<9;r++){
			for (int k=0;k<9;k++){
				sudokuPanel.setBold(r,k,s.getSudokuValue(r,k)!=0);
			}
		}
		
	}
	
	
	public static void main(String[] args) {
        // Use System Look and Feel
        try { 
        	
        	UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
        	
        }
        catch (Exception ex) { ex.printStackTrace(); }
        new Sudoku();
    }


	@Override
	public void valuedChanged(int intValue) {
		game.setValue(current.getRow(),current.getCol(),intValue);
		current.setValue(intValue);
		if (game.won()){
			JOptionPane.showMessageDialog(this, "You Won!!!");
		}
	}


	@Override
	public void LabelClicked(TextFields label) {
		if (current!=null) current.setColor(o);
		current=label;
		o=current.getBackground();
		current.setColor(Color.WHITE);
		buttonPaneln.setValue(game.getSudokuValue(label.getRow(), label.getCol()));
	}


	

}
